package com.fuze.questionnaire.controller;

import com.fuze.questionnaire.common.Advice;
import com.fuze.questionnaire.common.Result;
import com.fuze.questionnaire.service.InsertionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;

/**
 * @author sepc_
 * @date 10/15/2021 14:01
 * 问卷数据插入接口
 */
@Slf4j
@RestController
@ApiIgnore
@RequiredArgsConstructor
public class InsertionController {

    final InsertionService insertionService;

    @PostMapping("/insertFromExcel")
    public Result insertFromExcel(MultipartFile excel) {
        try {
            byte[] bytes = excel.getBytes();
            return Result.ok(insertionService.insertFromExcel(bytes));
        } catch (IOException e) {
            e.printStackTrace();
            log.error("error message : {}", e.getMessage());
            return Result.err(Advice.FAILED_READ_DATA);
        }
    }
}
